/**
 * auth: zhukaijun
 * date: 2021-6-10
 * des: common utils
 */
// 加法
export function accAdd(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length; // 获取a的小数位长度
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length; // 获取b的小数位长度
    } catch (f) {
        d = 0;
    }
    //先求e，把a、b 同时乘以e转换成整数相加，再除以e还原
    return e = Math.pow(10, Math.max(c, d)), (accMul(a, e) + accMul(b, e)) / e;
}

// 乘法
export function accMul(a, b) {
    var c = 0,
        d = a.toString(), // 转换为字符串 
        e = b.toString(); //
    try {
        c += d.split(".")[1].length; // c 累加a的小数位长度
    } catch (f) {
    }
    try {
        c += e.split(".")[1].length; // c 累加b的小数位长度
    } catch (f) {
    }
    // 转换为整数相乘，再除以10^c ,移动小数点，还原，利用整数相乘不会丢失精度
    return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}

// 减法
export function accSub(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length; // 获取a的小数位长度
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length; // 获取b的小数位长度
    } catch (f) {
        d = 0;
    }
    // 和加法同理
    return e = Math.pow(10, Math.max(c, d)), (accMul(a, e) - accMul(b, e)) / e;
}

// 除法
export function accDiv(a, b) {
    var c, d, e = 0,
        f = 0;
    try {
        e = a.toString().split(".")[1].length;
    } catch (g) {
    }
    try {
        f = b.toString().split(".")[1].length;
    } catch (g) {
    }
    // 同理，转换为整数，运算后，还原
    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), accMul(c / d, Math.pow(10, f - e));
}

// 补零
export function formatNumber(value, num) {
    // value:数字;num:补零个数
    let _value = value.toString();
    let _dot = _value.indexOf(".");
    let _valueLen = _value.length;
    if (num == 0) {
        if (_dot != -1) {
            _value = _value.substring(0, _dot);
        }
    } else { //如果没有小数点
        if (_dot == -1) {
            _value = _value + ".";
            for (let i = 1; i <= num; i++) {
                _value = _value + "0";
            }
        } else { //有小数点，超出位数自动截取，否则补0
            _value = _value.substring(0, _dot + num + 1);
            for (let i = _valueLen; i <= _dot + num; i++) {
                _value = _value + "0";
            }
        }
    }
    return _value;
}

// 数组根据字段去重
export function removeRepeat(name, arr) {
    // name:字段; arr:数组
    const res = new Map();
    return arr.filter((a) => !res.has(a[name]) && res.set(a[name], 1))
}

// 截取时间(年月日时分秒)
export function cutOutDateTime(str, format) {
    // str:时间字符串(例:2022-01-01 12:00:00或者2022/01/01 12:00:00)
    // format-> Y:年;M:月;D:日;h:时;m:分;s:秒;(要区分大小写)
    if (str) {
        let symbol = '';
        let reg = new RegExp('/');
        let reg2 = new RegExp('-');
        if (reg.test(str)) {
            symbol = '/';
        }
        if (reg2.test(str)) {
            symbol = '-';
        }
        let timeArr = str.replace(" ", ":").replace(/\:/g, symbol).split(symbol);
        let timeStr = "";
        // 年
        if (format == 'Y') {
            timeStr = "" + timeArr[0]
        }
        // 月
        if (format == 'M') {
            timeStr = "" + timeArr[0] + symbol + timeArr[1];
        }
        // 日
        if (format == 'D') {
            timeStr = "" + timeArr[0] + symbol + timeArr[1] + symbol + timeArr[2];
        }
        // 时
        if (format == 'h') {
            timeStr = "" + timeArr[0] + symbol + timeArr[1] + symbol + timeArr[2] + " " + timeArr[3];
        }
        // 分
        if (format == 'm') {
            timeStr = "" + timeArr[0] + symbol + timeArr[1] + symbol + timeArr[2] + " " + timeArr[3] + ":" + timeArr[4];
        }
        // 秒
        if (format == 's') {
            timeStr = "" + timeArr[0] + symbol + timeArr[1] + symbol + timeArr[2] + " " + timeArr[3] + ":" + timeArr[4] + ":" + timeArr[5];
        }
        return timeStr
    }
}

// 链接后缀名截取
export function getUrlSuffix(url) {
    // url:链接地址
    if (url) {
        let path = "";
        if (url.indexOf('.cn/') !== -1) {
            path = (url.split('.')[url.split('.').length - 2] + '.' + url.split('.')[url.split('.').length - 1]).substring(3);
        } else if (url.indexOf('.com/') !== -1) {
            path = (url.split('.')[url.split('.').length - 2] + '.' + url.split('.')[url.split('.').length - 1]).substring(4);
        }
        let index = path.lastIndexOf("\.");
        path = path.substring(index + 1, path.length);
        return path;
    }
}

export function getParams() {
    let query = window.location.search.substring(1), vars = query.split("&"), data = {}
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        data[pair[0]] = pair[1];
    }
    return data;

}

// 两个时间字符串相减计算时间差
export function getDateDiff(startTime, endTime) {
    // startTime:开始时间;endTime:结束时间
    //将日期字符串转换为时间戳
    var sTime = new Date(startTime).getTime(); //开始时间
    var eTime = new Date(endTime).getTime(); //结束时间
    //作为除数的数字
    var divNumSecond = 1000;
    var divNumMinute = 1000 * 60;
    var divNumHour = 1000 * 3600;
    var divNumDay = 1000 * 3600 * 24;
    const day = parseInt((eTime - sTime) / parseInt(divNumDay))
    const hour = parseInt(((eTime - sTime) % parseInt(divNumDay)) / parseInt(divNumHour))
    const minute = parseInt((parseInt(((eTime - sTime) % parseInt(divNumDay)) % parseInt(divNumHour))) / parseInt(divNumMinute))
    const second = ((parseInt(((eTime - sTime) % parseInt(divNumDay)) % parseInt(divNumHour))) % parseInt(divNumMinute)) / parseInt(divNumSecond)
    // const str = day + '天' + hour + '小时' + minute + '分' + second + '秒'
    return day
}

//年月日
export function formatDate(dateval) {
    let restr;
    if (typeof dateval === 'string' && dateval.indexOf('-')) {
        restr = dateval.replace(/\-/g, "/");
    } else {
        restr = dateval;
    }
    var date = new Date(restr);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD + " " + hh + mm + ss;
};